
package com.ag.wzp.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


import com.ag.admin.service.impl.UserServiceImpl;
import com.ag.bean.TbDept;
import com.ag.bean.TbEvent;
import com.ag.bean.TbSched;
import com.ag.bean.TbUser;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.DateUtil;
import com.ag.common.EventUtil;
import com.ag.common.IDUtil;
import com.ag.common.IPermissionService;
import com.ag.common.KeyUtil;
import com.ag.wsq.service.impl.OneSchedServiceImpl;
import com.ag.wzp.service.impl.EventServiceImpl;
import com.ag.zhh.service.ContactLogService;

/**
 * @author 温镇澎
 * 
 */
@Scope("prototype")
@Controller("eventAction")
public class EventAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(EventAction.class);
	
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}
	
	@Resource(name="eventServiceImpl")
	private EventServiceImpl eventServiceImpl;
	@Resource(name="oneSchedServiceImpl")
	private OneSchedServiceImpl oneSchedServiceImpl;
	@Resource(name="ContactLogService")
	private ContactLogService contactLogService;
	@Resource(name = "userServiceImpl")
	private UserServiceImpl userService;
	
	private TbEvent event = new TbEvent();
	private List<TbEvent> events;
	private List<TbDept> depts;
	//private String selDept;//报送中选择的部门ID
	private String submitTime;//报送时间
	//private String elId;//事件等级ID
	String ajaxRst;
	private String EId;//事件ID
	private String daContent;//处置建议内容
	private TbUser leader;
	private String leaderId;//带班领导ID
	private String smsContent;//通知内容
	
	private String isNotify;//是否通知了领导；
	/**
	 * 保存事件,事件的状态写死在wzp/js/accept.js
	 * @return
	 * @since 9.30
	 */
	public String save(){
		Boolean b = eventServiceImpl.save(event);
		
		List<TbSched> scheds = this.oneSchedServiceImpl.daiban();
		if(scheds.size()>0){
			TbSched sched = scheds.get(0);
			leader = sched.getTbUser();
		}
		ajaxRst = b.toString();
		return "eventSave";
	}
	
	//modified by czy 得到当前的值班领导，用于再次提交
	public String getdaiban(){
		ajaxRst = "true";
		List<TbSched> scheds = this.oneSchedServiceImpl.daiban();
		if(scheds.size()>0){
			TbSched sched = scheds.get(0);
			leader = sched.getTbUser();
		}
		return "getDaiban";
	}
	//czy end 
	/**
	 * 根据EId获取Event，用于查看事件详情
	 * @return
	 */
	public String findById(){
		logger.info("do get !"+ event.getEId());
		String eid = event.getEId();
		if(eid!=null&&eid.length()>0){
			//事件处理过程中需要session里有EId
			this.sessionMap.put(EventUtil.PROCESSING_EVENT_ID, eid);
			event=eventServiceImpl.findById(eid);
			this.sessionMap.put(EventUtil.PROCESSING_EVENT_STATUS, event.getTbEventStatus().getTbesId());
			return "findByEID";
		}else{
			return ERROR;
		}
	}
	
	/**
	 * 根据EId获取Event，用于事件补充
	 * @return
	 */
	public String findEventToMackup(){
		logger.info("do get !"+ event.getEId());
		String eid = event.getEId();
		if(eid!=null&&eid.length()>0){
			//事件处理过程中需要session里有EId
			this.sessionMap.put(EventUtil.PROCESSING_EVENT_ID, eid);
			event=eventServiceImpl.findById(eid);
			this.sessionMap.put(EventUtil.PROCESSING_EVENT_STATUS, event.getTbEventStatus().getTbesId());
			return "findEventToMackup";
		}else{
			return ERROR;
		}
	}
	
	public String edit(){
		logger.info("EId = "+EId);
		if(EId!=null&&EId.length()>0){
			event=eventServiceImpl.findById(EId);
			return "eventEdit";
		}else{
			return ERROR;
		}
	}
	
	public void update(){
		Boolean b = eventServiceImpl.update(event);
		this.outString(b.toString());
	}
	
	public void delete(){
		if(EId!=null&&EId.length()>0){
			Boolean b = eventServiceImpl.delete(EId);
			//hmk start
			//this.sessionMap.remove(EventUtil.PROCESSING_EVENT_ID);
			//this.sessionMap.remove(EventUtil.PROCESSING_EVENT_STATUS);
			//hmk end
			this.outString(b.toString());
		}else{
			this.outString("false");
		}
	}
	/**
	 * 
	 * @return
	 */
	public String addByCall(){
		event.setESubmiterContact("812345678");
		event.setESubmiter("李咏");
		submitTime = DateUtil.DateToString(new Date());
		//user = eventServiceImpl.findAllUser().get(0);
		return "callOK";
	}
	/*
	 * 通知带班领导
	 */
	public String notifyToLeader() {
		this.ajaxRst = "true";
		TbUser user = pms.getCurrentUser(sessionMap);
		TbUser leader = null;
		try {
			leader = this.userService.findById(leaderId);
		} catch (Exception e) {
			e.printStackTrace();
			this.ajaxRst = "false";
		}
		if (leader != null) {
			Boolean b = this.contactLogService.addOneMessageContactLogByEvent(user.getUName(), leader.getUName(), leader.getUId(), leader.getUPhone(), smsContent, EId);
			this.ajaxRst = b.toString();
		} else {
			this.ajaxRst = "false";
		}
		return "ajaxRst";
	}
	/**
	 * 判断是否通知了领导
	 * @return
	 */
	public String isNotify(){
		for(int i=0;i<10;i++){
			System.out.println("**");
		}
		System.out.println(EId);
		if(eventServiceImpl.isNotify(EId))
			this.setIsNotify("true");
		else this.setIsNotify("false");
		return "isNotify";
	}
	
	public TbEvent getEvent() {
		return event;
	}
	public void setEvent(TbEvent event) {
		this.event = event;
	}
	public List<TbEvent> getEvents() {
		return events;
	}
	public void setEvents(List<TbEvent> events) {
		this.events = events;
	}
	public List<TbDept> getDepts() {
		return depts;
	}
	public void setDepts(List<TbDept> depts) {
		this.depts = depts;
	}
	public String getSubmitTime() {
		return submitTime;
	}
	public void setSubmitTime(String submitTime) {
		this.submitTime = submitTime;
	}
	public String getEId() {
		return EId;
	}
	public void setEId(String eId) {
		EId = eId;
	}
	public String getDaContent() {
		return daContent;
	}
	public void setDaContent(String daContent) {
		this.daContent = daContent;
	}
	public String getAjaxRst() {
		return ajaxRst;
	}
	public void setAjaxRst(String ajaxRst) {
		this.ajaxRst = ajaxRst;
	}
	public TbUser getLeader() {
		return leader;
	}
	public void setLeader(TbUser leader) {
		this.leader = leader;
	}

	public String getLeaderId() {
		return leaderId;
	}

	public void setLeaderId(String leaderId) {
		this.leaderId = leaderId;
	}

	public String getSmsContent() {
		return smsContent;
	}

	public void setSmsContent(String smsContent) {
		this.smsContent = smsContent;
	}

	public String getIsNotify() {
		return isNotify;
	}

	public void setIsNotify(String isNotify) {
		this.isNotify = isNotify;
	}

}
